package com.gupao.study.proxy.cglibProxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor {
	private Object target;
	public CglibProxy(Object target) {
		this.target = target;
	}
	
	public Object getInstance() {
		Enhancer en =new Enhancer();
		en.setSuperclass(target.getClass());
		//en执行方法，回调MethodInterceptor的子类实现方法
		en.setCallback(this);
		return en.create();
	}

	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("房屋中介：帮忙找房子");
		method.invoke(target, args);
		System.out.println("找到了140的毛坯房;");
		return null;
	}
}
